package com.ocm.core.dao.jpa;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManagerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.ocm.core.dao.StorageDao;
import com.ocm.core.domain.Storage;

@Repository("storageDao")
public class JpaStorageDao extends JpaGenericDao<Storage> implements StorageDao {

   @Autowired
   public JpaStorageDao(@Qualifier("ocmEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
      super(Storage.class);
      super.setEntityManagerFactory(entityManagerFactory);
   }

   @SuppressWarnings("unchecked")
   public List<Storage> findAll() {
      List<Storage> storages = this.getJpaTemplate().findByNamedQuery(Storage.FIND_ALL);
      return storages;
   }

   @SuppressWarnings("unchecked")
   public Storage findName(String name) {
      Map<String, Object> paras = new HashMap<String, Object>();
      paras.put(Storage.NAME_PARA, name);
      List<Storage> storages = this.getJpaTemplate().findByNamedQueryAndNamedParams(Storage.FIND_NAME, paras);
      if (storages.size() > 0){
         return storages.get(0);
      }
      return null;
   }
}
